package com.lxl.xm.service;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lxl.xm.exception.BusinessException;

import com.lxl.xm.mapper.AdvertiserMapper;
import com.lxl.xm.model.entity.Advertiser;
import com.lxl.xm.model.vo.AdvertiserVO;
import com.lxl.xm.model.vo.AdvertiserVOToAdvertiser;
import com.lxl.xm.model.vo.AdvertiserVOToAdvertiser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Random;
@Service

public class MailService {
    @Autowired
    private JavaMailSender mailSender;
    @Autowired
    private AdvertiserMapper advertiserMapper;
    @Value("${spring.mail.username}")
    private String from;

    /**
     * 给前端输入的邮箱，发送验证码
     * @param email
     * @param session
     * @return
     */
    public boolean sendMimeMail( String email, HttpSession session) {
        try {
            SimpleMailMessage mailMessage = new SimpleMailMessage();

            mailMessage.setSubject("验证码邮件");//主题
            //生成随机数
            String code = randomCode();

            //将随机数放置到session中
            session.setAttribute("email",email);
            session.setAttribute("code",code);

            mailMessage.setText("您收到的验证码是："+code);//内容

            mailMessage.setTo(email);//发给谁

            mailMessage.setFrom(from);//你自己的邮箱

            mailSender.send(mailMessage);//发送
            return  true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 随机生成6位数的验证码
     * @return String code
     */
    public String randomCode(){
        StringBuilder str = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < 6; i++) {
            str.append(random.nextInt(10));
        }
        return str.toString();
    }

    /**
     * 检验验证码是否一致
     * @param advertiserVO
     * @param session
     * @return
     */
    public boolean registered(AdvertiserVO advertiserVO, HttpSession session){
        //获取session中的验证信息
        String email = (String) session.getAttribute("email");
        String code = (String) session.getAttribute("code");
        String name = advertiserVO.getAdvertiserName();
        String password = advertiserVO.getAdvertiserPassword();
        //验证邮箱是否已注册
        LambdaQueryWrapper<Advertiser> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Advertiser::getAdvertiserName, advertiserVO.getAdvertiserName());

        boolean isAdvertiserExists = !advertiserMapper.selectList(lqw).isEmpty();
        if(isAdvertiserExists){
            throw new BusinessException(-1,"该邮箱已被注册！");
        }

        //获取表单中的提交的验证信息
        String voCode = advertiserVO.getCode();

        //如果email数据为空，或者不一致，注册失败
        if (email == null || email.isEmpty()||name==null||name.isEmpty()||password==null||password.isEmpty()){
            //return "error,请重新注册";
            throw new BusinessException(-2,"请输入完整信息！");
        }else if (!code.equals(voCode)){
            //return "error,请重新注册";
            throw new BusinessException(-3,"验证码错误！");
        }
        advertiserVO.setEmail(email);
        //保存数据
        Advertiser advertiser = AdvertiserVOToAdvertiser.toAdvertiser(advertiserVO);

        //将数据写入数据库
        advertiserMapper.registerAdvertiser(advertiser);

        //跳转成功页面
        return true;
    }

    /**
     * 通过输入email查询password，然后比较两个password，如果一样，登录成功
     * @param email
     * @param password
     * @return
     */

    public boolean loginIn(String email, String password){

        Advertiser advertiser = advertiserMapper.queryByEmail(email);
        if(advertiser==null){
            throw new BusinessException(-4,"该邮箱未被注册，请先进行注册！");
        }

        if(!advertiser.getAdvertiserPassword().equals(password)){
            throw new BusinessException(-5,"账号或密码错误！");
        }
        System.out.println("登录成功:数据库密码是："+advertiser.getAdvertiserPassword());
        return true;
    }
}
